package ymr.server;

import java.io.*;
import java.net.*;

import ymr.game.Game;

public class Server extends Thread{

    static Socket clientSocket = null;
    static ServerSocket serverSocket = null;
    static Client clients[] = new Client[9]; // max 10 klienti       
	
    public Server(){
    	start();
    }
    
    public void run(){
		int port = 2222;

		System.out.println("Server kuulab pordil: " + port);

		try {
		    serverSocket = new ServerSocket(port);

		    Game game = new Game();
		    
			while(true){
			    try {
					clientSocket = serverSocket.accept();
					for(int i = 0; i <= clients.length; i++){
					    if(clients[i]==null){
						    (clients[i] = new Client(clientSocket,clients,game)).start();
						    break;
						}
					}
				}catch (IOException e) {
					System.out.println(e);
					System.out.println("kohti pole?");
				}
			}
        }
        catch (IOException e){
        	System.out.println(e);
        	// siis kui port juba kasutuses on nt
        	System.out.println("Server ei funktsi?");
        }
    }
	
}
